package com.fbinggun.fbinggunutil.fgive;

import com.fbinggun.fbinggunutil.API.ItemUtil;
import com.fbinggun.fbinggunutil.FBinggunUtil;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FgiveAPI {

    Fgive fgive;

    protected FgiveAPI(Fgive fgive) {
        this.fgive = fgive;
    }

    public  ItemStack getItemStack(String name) {
        return fgive.util.getItemStack(name);
    }

    public  List<String> getItemStackList() {
        try {
            return fgive.getFgiveItemSQL().getItemList();
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public Map<String,ItemStack> getItemStackMap() {
        try {
            return fgive.getFgiveItemSQL().getItemMap();
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isExistence(String name) {
       return getItemStackList().contains(name);
    }

    public  void giveNonePrompt(Player player, ItemStack itemStack){
        if(player.getInventory().firstEmpty()==-1){
            player.getLocation().getWorld().dropItem(player.getLocation(),itemStack);
            player.sendMessage("§c背包已满掉落到了地上");
        }else {
            player.getInventory().addItem(itemStack);
        }
    }



    public  void give(Player player, String name){
        //赐予物品 有提示
        ItemStack item =fgive.util.getItemStack(name);
        if(item==null) {
            return;
        }
        item.setAmount(1);
        if(player.getInventory().firstEmpty()==-1){
            player.getLocation().getWorld().dropItem(player.getLocation(),item);
            player.sendMessage(getMsg("GiveMsg",item));
            player.sendMessage(getMsg("BackPack_Full",item));
        }else {
            player.getInventory().addItem(item);
            player.sendMessage(getMsg("GiveMsg",item));
        }
    }

    private  String getMsg(String key,ItemStack itemStack){
        String msg = fgive.getLang().getLang("Lang."+key);
        String names= ItemUtil.getDisplayName(itemStack);
        msg=msg.replaceAll("%Name%", names);
        msg=msg.replaceAll("%Amount%", String.valueOf(itemStack.getAmount()));
        return msg;
    }

    public  void give(Player player, String name,int count){
        ItemStack item =fgive.util.getItemStack(name);
        if(item==null) {
            return;
        }
        item.setAmount(count);
        if(player.getInventory().firstEmpty()==-1){
            player.getLocation().getWorld().dropItem(player.getLocation(),item);
            player.sendMessage(getMsg("GiveMsg",item));
            player.sendMessage(getMsg("BackPack_Full",item));
        }else {
            player.getInventory().addItem(item);
            player.sendMessage(getMsg("GiveMsg",item));
        }
    }


    public  void give(Player player, ItemStack item,int count){
        if(item==null) {
            return;
        }
        item.setAmount(count);
        if(player.getInventory().firstEmpty()==-1){
            player.getLocation().getWorld().dropItem(player.getLocation(),item);
            player.sendMessage(getMsg("GiveMsg",item));
            player.sendMessage(getMsg("BackPack_Full",item));
        }else {
            player.getInventory().addItem(item);
            player.sendMessage(getMsg("GiveMsg",item));
        }
    }

    public void save(String id,ItemStack item){
        try {
            fgive.util.saveItem(item,id);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
